﻿using Entity.Models.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Utils;

namespace Common.Utils
{
    /// <summary>
    /// 迁移数据初始化类
    /// 
    /// </summary>
    public class DataInitialization
    {
        /// <summary>
        /// 用户管理
        /// </summary>
        /// <returns></returns>
        public UserInfo UserInfo()
        {
            return new UserInfo()
            {
                Id = "0682a5f4-dde4-4a7f-9de7-5111ebdfd686",
                Account = "admin",
                PassWord = MD5.md5Encrypt("123"),
                PhoneNum = "13478292101",
                Sex = 1,
                Email = "547335454@qq.com",
                IsAdmin = true,
                CreateTime = DateTime.Now,
                UserName = "蟹老板"
            };
        }

        /// <summary>
        /// 菜单管理
        /// </summary>
        /// <returns></returns>
        public List<MenuInfo> MenuInfos()
        {
            List<MenuInfo> menuInfoslist = new List<MenuInfo>();
            //常规管理
            MenuInfo menuInfo = new MenuInfo()
            {
                Id = "e391d559-047e-4783-9437-cd8da512c706",
                Title = "常规管理",
                Level = 0,
                Sort = 1000,
                ParentId = "0",
                Icon = "fa fa-address-book",
                Target = "_self",
                CreateTime = DateTime.Now
            };
            menuInfoslist.Add(menuInfo);
            //系统管理
            MenuInfo menuInfo1 = new MenuInfo()
            {
                Id = "205d2865-badf-45d1-ae2f-9e1503da19e8",
                Title = "系统管理",
                Level = 1,
                Sort = 3000,
                ParentId = "e391d559-047e-4783-9437-cd8da512c706",
                Icon = "fa fa-gears",
                Target = "_self",
                CreateTime = DateTime.Now
            };
            menuInfoslist.Add(menuInfo1);
            //菜单管理
            MenuInfo menuInfo2 = new MenuInfo()
            {
                Id = "3f61b67e-5810-4558-84c0-09bfd30e02b8",
                Title = "菜单管理",
                Level = 2,
                Sort = 3400,
                Href = "/MenuInfo/index",
                ParentId = "205d2865-badf-45d1-ae2f-9e1503da19e8",
                Icon = "fa fa-bug",
                Target = "_self",
                CreateTime = DateTime.Now
            };
            menuInfoslist.Add(menuInfo2);
            //用户管理
            MenuInfo menuInfo3 = new MenuInfo()
            {
                Id = "2916de12-a7cd-4446-91dd-a337ab64b847",
                Title = "用户管理",
                Level = 2,
                Sort = 3100,
                Href = "/User/index",
                ParentId = "205d2865-badf-45d1-ae2f-9e1503da19e8",
                Icon = "fa fa-meh-o",
                Target = "_self",
                CreateTime = DateTime.Now
            };
            menuInfoslist.Add(menuInfo3);
            //角色管理
            MenuInfo menuInfo4 = new MenuInfo()
            {
                Id = "288e99b5-31ab-4989-9f0c-a8016108d7ce",
                Title = "角色管理",
                Level = 2,
                Sort = 3300,
                Href = "/RoleInfo/index",
                ParentId = "205d2865-badf-45d1-ae2f-9e1503da19e8",
                Icon = "fa fa-sliders",
                Target = "_self",
                CreateTime = DateTime.Now
            };
            menuInfoslist.Add(menuInfo4);
            //其它管理
            MenuInfo menuInfos = new MenuInfo()
            {
                Id = "b1d5fb3f-835e-4906-b3e2-4766ccefde08",
                Title = "其它管理",
                Level = 0,
                Sort = 2000,
                ParentId = "0",
                Icon = "fa fa-address-book",
                Target = "_self",
                CreateTime = DateTime.Now
            };
            menuInfoslist.Add(menuInfos);
            //角色管理
            MenuInfo menuInfos1 = new MenuInfo()
            {
                Id = "799f4848-948d-4cbc-95a8-ce1fb9203ea6",
                Title = "日志",
                Level = 2,
                Sort = 3000,
                Href = "/RoleInfo/index",
                ParentId = "b1d5fb3f-835e-4906-b3e2-4766ccefde08",
                Icon = "fa fa-wheelchair",
                Target = "_self",
                CreateTime = DateTime.Now
            };
            menuInfoslist.Add(menuInfos1);
            return menuInfoslist;
        }

        /// <summary>
        /// 角色管理
        /// </summary>
        /// <returns></returns>
        public RoleInfo roleInfo()
        {
            return new RoleInfo()
            {
                Id = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                RoleName = "管理员",
                Description = "管理",
                CreateTime = DateTime.Now
            };
        }

        /// <summary>
        /// 绑定角色
        /// </summary>
        /// <returns></returns>
        public R_UserInfo_RoleInfo r_UserInfo_RoleInfo()
        {
            R_UserInfo_RoleInfo r_UserInfo_RoleInfo = new R_UserInfo_RoleInfo()
            {
                Id = "6978c211-f029-4936-ad1e-b41f875ec949",
                UserId = "0682a5f4-dde4-4a7f-9de7-5111ebdfd686",
                RoleId = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                CreateTime = DateTime.Now
            };
            return r_UserInfo_RoleInfo;
        }

        /// <summary>
        /// 绑定菜单
        /// </summary>
        /// <returns></returns>
        public List<R_RoleInfo_MenuInfo> R_RoleInfo_MenuInfo()
        {
            List<R_RoleInfo_MenuInfo> R_RoleInfo_MenuInfos = new List<R_RoleInfo_MenuInfo>();
            R_RoleInfo_MenuInfo r_RoleInfo_MenuInfo = new R_RoleInfo_MenuInfo()
            {
                Id = "01a940cc-d103-4791-b263-9fbdb37ad01a",
                MenuId = "3f61b67e-5810-4558-84c0-09bfd30e02b8",
                RoleId = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                CreateTime = DateTime.Now,
            };
            R_RoleInfo_MenuInfos.Add(r_RoleInfo_MenuInfo);
            R_RoleInfo_MenuInfo r_RoleInfo_MenuInfo1 = new R_RoleInfo_MenuInfo()
            {
                Id = "02813e38-ef2f-4f53-82ff-c95595f2710c",
                MenuId = "799f4848-948d-4cbc-95a8-ce1fb9203ea6",
                RoleId = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                CreateTime = DateTime.Now,
            };
            R_RoleInfo_MenuInfos.Add(r_RoleInfo_MenuInfo1);
            R_RoleInfo_MenuInfo r_RoleInfo_MenuInfo2 = new R_RoleInfo_MenuInfo()
            {
                Id = "1cd06b3c-08c1-462b-aacc-5793e0916ffe",
                MenuId = "b1d5fb3f-835e-4906-b3e2-4766ccefde08",
                RoleId = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                CreateTime = DateTime.Now,
            };
            R_RoleInfo_MenuInfos.Add(r_RoleInfo_MenuInfo2);
            R_RoleInfo_MenuInfo r_RoleInfo_MenuInfo3 = new R_RoleInfo_MenuInfo()
            {
                Id = "60356f3a-eb47-431f-b634-b5a805c39e0b",
                MenuId = "288e99b5-31ab-4989-9f0c-a8016108d7ce",
                RoleId = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                CreateTime = DateTime.Now,
            };
            R_RoleInfo_MenuInfos.Add(r_RoleInfo_MenuInfo3);
            R_RoleInfo_MenuInfo r_RoleInfo_MenuInfo4 = new R_RoleInfo_MenuInfo()
            {
                Id = "b413118d-7618-4d2f-8b80-3fd6614403a9",
                MenuId = "205d2865-badf-45d1-ae2f-9e1503da19e8",
                RoleId = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                CreateTime = DateTime.Now,
            };
            R_RoleInfo_MenuInfos.Add(r_RoleInfo_MenuInfo4);
            R_RoleInfo_MenuInfo r_RoleInfo_MenuInfo5 = new R_RoleInfo_MenuInfo()
            {
                Id = "b8195313-8813-4578-92df-7fed1a9bf7d0",
                MenuId = "e391d559-047e-4783-9437-cd8da512c706",
                RoleId = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                CreateTime = DateTime.Now,
            };
            R_RoleInfo_MenuInfos.Add(r_RoleInfo_MenuInfo5);
            R_RoleInfo_MenuInfo r_RoleInfo_MenuInfo6 = new R_RoleInfo_MenuInfo()
            {
                Id = "f158814f-6129-42fc-a54a-725377116d2a",
                MenuId = "2916de12-a7cd-4446-91dd-a337ab64b847",
                RoleId = "022e26dc-662e-4564-b04e-36fee9bf58c4",
                CreateTime = DateTime.Now,
            };
            R_RoleInfo_MenuInfos.Add(r_RoleInfo_MenuInfo6);
            return R_RoleInfo_MenuInfos;
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        public void InitDB()
        {
            var contextOptions = new DbContextOptionsBuilder<RepositorySysContext>()
                                        .UseSqlServer(@"Data Source =.; Initial Catalog = RepositorySys; User ID = sa; Password = 123")
                                        .Options;

            //启动项目之前，做数据库初始化操作
            RepositorySysContext dbContext = new RepositorySysContext(contextOptions);
            //如果有数据库就删除
            dbContext.Database.EnsureDeleted();
            //如果没有数据库就创建
            dbContext.Database.EnsureCreated();
            
            //用户管理
            dbContext.UserInfos.Add(UserInfo());
            //菜单管理
            dbContext.RoleInfo.Add(roleInfo());
            //菜单管理
            foreach (var item in MenuInfos())
            {
                dbContext.MenuInfo.Add(item);
            }
            //绑定角色(中间表)
            dbContext.R_UserInfo_RoleInfo.Add(r_UserInfo_RoleInfo());
            //绑定菜单(中间表)
            foreach (var item in R_RoleInfo_MenuInfo())
            {
                dbContext.R_RoleInfo_MenuInfo.Add(item);
            }

            dbContext.SaveChanges();
        }
    }
}
